/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Jan 3, 2010 9:23:09 PM $
 *
 * Author: Eric Yang
 * Date  : Jan 3, 2010 9:23:09 PM
 *
 */
package com.gooex.web.action.gbuy.order;

import org.c4j.system.web.action.Protected;

import com.gooex.domain.base.Category;
import com.gooex.domain.base.Order;
import com.gooex.domain.base.OrderCustomer;
import com.gooex.domain.base.OrderVendor;
import com.gooex.service.base.exception.ExceptionCode;


/**
 * @author Eric Yang
 * @version 1.0
 */
public class OrderCustomerAction extends BaseOrderAction implements Protected {

	private String categoryId;

	private String orderId;

	private String customerId;

	private String vendorId;

	private Category category;

	private Order order;

	private OrderCustomer orderCustomer;

	private OrderVendor orderVendor;

	public String createOrder() throws Exception {
		category = categoryManager.getCategory(categoryId);
		if (category == null) {
			return INPUT;
		}
		return SUCCESS;
	}

	public String voteOrder() throws Exception {
		order = orderManager.getOrder(orderId);
		if (order == null) {
			addActionError(ExceptionCode.ORDER_NOT_EXISTS);
			return ERROR;
		}
		orderCustomer = orderManager.getOrderCustomer(order, customerId);
		orderVendor = orderManager.getOrderVendor(order, vendorId);
		if (orderCustomer == null || orderVendor == null) {
			addActionError(ExceptionCode.ORDER_NOT_EXISTS);
			return ERROR;
		}

		if (!orderCustomer.getUser().getId().equals(getCurrentUser().getId())) {
			addActionError(ExceptionCode.SYSTEM_ACCESS_ERROR);
			return ERROR;
		}

		orderCustomer.setVotedVendor(orderVendor);
		orderManager.saveOrUpdate(orderCustomer);

		return SUCCESS;
	}

	public String viewCustomerAddress() throws Exception {
		order = orderManager.getOrder(orderId);
		if (order == null) {
			addActionError(ExceptionCode.ORDER_NOT_EXISTS);
			return ERROR;
		}
		orderCustomer = orderManager.getOrderCustomer(order, customerId);
		orderVendor = orderManager.getOrderVendor(order, vendorId);
		if (orderCustomer == null || orderVendor == null) {
			addActionError(ExceptionCode.SYSTEM_ACCESS_ERROR);
			return ERROR;
		}

		if (!getCurrentUser().getId().equals(orderVendor.getCompany().getUser().getId())) {
			addActionError(ExceptionCode.SYSTEM_ACCESS_ERROR);
			return ERROR;
		}

		return SUCCESS;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getVendorId() {
		return vendorId;
	}

	public void setVendorId(String vendorId) {
		this.vendorId = vendorId;
	}

	public Order getOrder() {
		return order;
	}

	public OrderCustomer getOrderCustomer() {
		return orderCustomer;
	}

	public OrderVendor getOrderVendor() {
		return orderVendor;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public String getCustomerId() {
		return customerId;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

}
